/**
  ******************************************************************************
  *
  * @file    jlx256128g.c
  * @author  Chen Long
  * @version V1.0.0
  * @date    2017/10/06
  * @brief   This file provides the jlx256128g jlx256128g handle function
  *
  ******************************************************************************
**/
#include "jlx256128g.h"
/*
*/
#define LCD_CS_PIN   HW_GPIO_OUTPUT_PIN_4
#define LCD_DC_PIN   HW_GPIO_OUTPUT_PIN_5
#define LCD_RST_PIN  HW_GPIO_OUTPUT_PIN_6

/****************************************************
                 write function
 ****************************************************/
static void write_data(uint8_t data)
{
  hw.gpio.output_high(LCD_DC_PIN);     //high for data
  hw.spiB.send(&data, 1);
  hw.timer.delay_micros(1);
}

/*
 */
static void write_buff(uint8_t *p_data, uint32_t num)
{
  hw.gpio.output_high(LCD_DC_PIN);    //high for data
  hw.spiB.send(p_data, num);
  hw.timer.delay_micros(1);
}

/*
 */
static void write_cmd(uint8_t cmd)
{
  hw.gpio.output_low(LCD_DC_PIN);    //low for cmd
  hw.spiB.send(&cmd, 1);
  hw.timer.delay_micros(1);
}


/********************************************
            api function
*********************************************/
void jlx256128g_set_address(uint32_t page0, uint32_t page1, uint32_t column0, uint32_t column1)
{
  page0 += 4;   //why?
  page1 += 4;   //why?
  write_cmd(0x15);     //Set Column Address
  write_data(column0);
  write_data(column1);
  write_cmd(0x75);     //Set Page Address
  write_data(page0);
  write_data(page1);
  write_cmd(0x30);     //Extension command 1
  write_cmd(0x5c);     //Write data to ddram
}
/*
*/
void jlx256128g_reset()
{
  hw.gpio.output_high(LCD_RST_PIN);
  hw.timer.delay_millis(100);
  hw.gpio.output_low(LCD_RST_PIN);  
  hw.timer.delay_millis(100);
  hw.gpio.output_high(LCD_RST_PIN);
  hw.timer.delay_millis(100);
}	

/*
 */	  
void jlx256128g_clear()  
{  
  jlx256128g_set_address(0, LCD_PAGE_MAX-1, 0, LCD_COLUMN_MAX-1);
  
  for(uint32_t i=0; i<LCD_PAGE_MAX; i++)  
  { 
    for(uint32_t j=0; j<LCD_COLUMN_MAX; j++)  
    {  
      write_data(0x00);
    }
  } 
}

/*
 */	  
void jlx256128g_clear_page(uint8_t page)  
{  
  jlx256128g_set_address(page, page, 0, LCD_COLUMN_MAX-1);
  
  for(uint32_t j=0; j<LCD_COLUMN_MAX; j++)  
  {  
    write_data(0x00);
  }
}
/*
 *
 */
void jlx256128g_display(jlx_display_t* p_dis)
{
  jlx256128g_set_address(p_dis->page0, p_dis->page1, p_dis->column0, p_dis->column1);
  write_buff((uint8_t*)p_dis->p_data, p_dis->data_num);
}

/*
 *
 */
void jlx256128g_init()
{
  hw.gpio.output_low(LCD_CS_PIN);  //cs
  hw.timer.delay_millis(1);
  
  jlx256128g_reset();
  
  write_cmd(0x30);    //EXT=0
  write_cmd(0x94);      //Sleep out
  write_cmd(0x31);      //EXT=1
  write_cmd(0xD7);      //Autoread disable
  write_data(0X9F);   //
  write_cmd(0x32);    //Analog SET
  write_data(0x00);   //OSC Frequency adjustment
  write_data(0x01);    //Frequency on booster capacitors->6KHz
  write_data(0x03);   //Bias=1/11
  write_cmd(0x20);    // Gray Level
  write_data(0x01);
  write_data(0x03);
  write_data(0x05);
  write_data(0x07);
  write_data(0x09);
  write_data(0x0b);
  write_data(0x0d);
  write_data(0x10);
  write_data(0x11);
  write_data(0x13);
  write_data(0x15);
  write_data(0x17);
  write_data(0x19);
  write_data(0x1b);
  write_data(0x1d);
  write_data(0x1f);
  write_cmd(0x30); //EXT=0
  write_cmd(0x75); //Page Address setting
  write_data(0X00); // XS=0
  write_data(0X14); // XE=159 0x28
  write_cmd(0x15); //Clumn Address setting
  write_data(0X00); // XS=0
  write_data(0Xff); // XE=256
  
  write_cmd(0xBC); //Data scan direction
  write_data(0x00); //MX.MY=Normal
  write_data(0xA6); //
  
  write_cmd(0xCA); //Display Control
  write_data(0X00); //
  write_data(0X9F); //Duty=160
  write_data(0X20); //Nline=off
  write_cmd(0xF0); //Display Mode
  write_data(0X10); //10=Monochrome Mode,11=4Gray
  write_cmd(0x81); //EV control
  write_data(0x3F); //VPR[5-0]
  write_data(0x04); //VPR[8-6]
  write_cmd(0x20); //Power control
  write_data(0x0B); //D0=regulator ; D1=follower ; D3=booste, on:1 off:0
  
  hw.timer.delay_micros(100);
  
  write_cmd(0xAF); //Display on
  
  jlx256128g_clear();
}



